import 'package:dailoz/constants.dart';
import 'package:dailoz/generated/l10n.dart';
import 'package:dailoz/main.dart';
import 'package:dailoz/service/task_service/task_service.dart';
import 'package:dailoz/size_config.dart';
import 'package:dailoz/entity/task_entity.dart';
import 'package:dailoz/widgets/cupertino_popup_menu.dart';
import 'package:flutter/material.dart';

class TaskWidget extends StatefulWidget {
  const TaskWidget({
    Key? key,
    this.margin = EdgeInsets.zero,
    this.padding = EdgeInsets.zero,
    required this.height,
    required this.width,
    required this.taskName,
    required this.taskTime,
    this.labelGroupPadding = EdgeInsets.zero,
    required this.taskLabel,
    this.moreOnTap,
    required this.taskId,
    this.parentFunction,
    this.isCanceled = false,
    required this.task,
  }) : super(key: key);

  final EdgeInsetsGeometry? margin;
  final EdgeInsetsGeometry? padding;
  final double? height;
  final double? width;
  final String? taskName;
  final String taskId;
  final String? taskTime;
  final EdgeInsetsGeometry? labelGroupPadding;
  final List<Widget>? taskLabel;
  final GestureTapCallback? moreOnTap;
  final Function? parentFunction;
  final bool? isCanceled;
  final TaskEntity task;

  @override
  State<TaskWidget> createState() => _TaskWidgetState();
}

class _TaskWidgetState extends State<TaskWidget> {
  Color? taskColor;
  Color? boxColor;

  List<PopupMenuEntry<dynamic>> list = [];

  void changeTaskColor() {
    BuildContext? context = navigatorKey.currentState?.overlay?.context;
    switch (widget.task.status) {
      case 1:
        taskColor = completedTaskColor;
        boxColor = completedTaskBoxColor;
        break;
      case 2:
        taskColor = cancelTaskColor;
        boxColor = cancelTaskBoxColor;
        list.add(
          PopupMenuItem(
            value: "enable",
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: <Widget>[
                Text(
                  S.of(context!).enable,
                  style: Theme.of(context).textTheme.bodyText2?.copyWith(
                        fontSize: SizeConfig.getProportionateFontSize(15.0),
                      ),
                ),
                Icon(
                  Icons.restart_alt_outlined,
                  color: Theme.of(context).textTheme.bodyText2?.color,
                  size: SizeConfig.getProportionateFontSize(20.0),
                ),
              ],
            ),
          ),
        );
        break;
      case 3:
        taskColor = pendingTaskColor;
        boxColor = pendingTaskBoxColor;
        list.add(
          PopupMenuItem(
            value: "disable",
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: <Widget>[
                Text(
                  S.of(context!).disable,
                  style: Theme.of(context).textTheme.bodyText2?.copyWith(
                        fontSize: SizeConfig.getProportionateFontSize(
                          15.0,
                        ),
                      ),
                ),
                Icon(
                  Icons.remove_circle_outline,
                  color: Theme.of(context).textTheme.bodyText2?.color,
                  size: SizeConfig.getProportionateFontSize(20.0),
                ),
              ],
            ),
          ),
        );
        break;
      case 4:
        taskColor = onGoingTaskColor;
        boxColor = onGoingTaskBoxColor;
        list.add(
          PopupMenuItem(
            value: "disable",
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: <Widget>[
                Text(
                  S.of(context!).disable,
                  style: Theme.of(context).textTheme.bodyText2?.copyWith(
                        fontSize: SizeConfig.getProportionateFontSize(
                          15.0,
                        ),
                      ),
                ),
                Icon(
                  Icons.remove_circle_outline,
                  color: Theme.of(context).textTheme.bodyText2?.color,
                  size: SizeConfig.getProportionateFontSize(20.0),
                ),
              ],
            ),
          ),
        );
        break;
    }
    list.add(
      PopupMenuItem(
        value: "edit",
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: <Widget>[
            Text(
              S.of(context!).modify,
              style: Theme.of(context).textTheme.bodyText2?.copyWith(
                    fontSize: SizeConfig.getProportionateFontSize(15.0),
                  ),
            ),
            Icon(
              Icons.edit,
              color: Theme.of(context).textTheme.bodyText2?.color,
              size: SizeConfig.getProportionateFontSize(20.0),
            ),
          ],
        ),
      ),
    );
    list.add(
      PopupMenuItem(
        value: "delete",
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: <Widget>[
            Text(
              S.of(context).delete,
              style: Theme.of(context).textTheme.bodyText2?.copyWith(
                    fontSize: SizeConfig.getProportionateFontSize(
                      15.0,
                    ),
                    color: Colors.red,
                  ),
            ),
            Icon(
              Icons.highlight_remove_outlined,
              color: Colors.red,
              size: SizeConfig.getProportionateFontSize(20.0),
            ),
          ],
        ),
      ),
    );
  }

  @override
  void initState() {
    super.initState();
    changeTaskColor();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: widget.padding,
      margin: widget.margin,
      height: SizeConfig.getProportionateScreenHeight(widget.height!),
      width: SizeConfig.getProportionateScreenWidth(widget.width!),
      decoration: BoxDecoration(
        color: boxColor,
        borderRadius: const BorderRadius.all(
          Radius.circular(15.0),
        ),
      ),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Container(
            margin: EdgeInsets.only(
              top: SizeConfig.getProportionateScreenHeight(10.0),
            ),
            padding: EdgeInsets.only(
              left: SizeConfig.getProportionateScreenWidth(10.0),
            ),
            width: SizeConfig.getProportionateScreenWidth(292.0),
            height: SizeConfig.getProportionateScreenHeight(60.0),
            decoration: BoxDecoration(
              border: Border(
                left: BorderSide(
                  width: 2,
                  color: taskColor!,
                ),
              ),
            ),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: <Widget>[
                    Expanded(
                      child: Text(
                        widget.taskName!,
                        style: TextStyle(
                          color: bottomTextButtonColor,
                          fontSize: SizeConfig.getProportionateFontSize(16.0),
                        ),
                        maxLines: 1,
                        softWrap: false,
                        overflow: TextOverflow.ellipsis,
                      ),
                    ),
                    Center(
                      child: CupertinoPopupMenu(
                          borderShape: const RoundedRectangleBorder(
                            borderRadius: BorderRadius.all(
                              Radius.circular(12.0),
                            ),
                          ),
                          onSelected: (value) {
                            TaskService.select(context, value, widget);
                          },
                          offset: const Offset(10.0, 30.0),
                          child: Icon(
                            Icons.more_vert,
                            size: SizeConfig.getProportionateFontSize(22.0),
                            color: Colors.black,
                          ),
                          itemBuilder: (BuildContext context) => list),
                    ),
                  ],
                ),
                SizedBox(
                  height: SizeConfig.getProportionateScreenHeight(8.0),
                ),
                Row(
                  children: <Widget>[
                    Text(
                      widget.taskTime!,
                      style: TextStyle(
                        color: const Color(0xff9aa8c7),
                        fontSize: SizeConfig.getProportionateFontSize(14.0),
                      ),
                    ),
                  ],
                ),
              ],
            ),
          ),
          SingleChildScrollView(
            scrollDirection: Axis.horizontal,
            child: Container(
              margin: widget.labelGroupPadding,
              child: Row(
                mainAxisAlignment: MainAxisAlignment.start,
                children: widget.taskLabel!,
              ),
            ),
          ),
        ],
      ),
    );
  }
}
